<?php

namespace api\modules\shop\controllers;

use common\models\ShoppingCart;
use common\models\SupplierGoods;
use common\models\SupplierGoodsAttr;
use common\service\ConstantService;
use common\service\GlobalUrlService;
use common\service\SupplierGoodsService;

class ShoppingCartController extends ShopBaseController
{
    /**
     * 添加进货单
     */
    public function actionAddShoppingCart(){
        if($this->isPost()){

            $data = $this->post();
            if(empty($data)){
                return $this->renderJson([],'参数错误',ConstantService::$response_fail);
            }
            $new_data = [];
            foreach ($data as $item){
                $item = explode(',',$item);
                $arr = [];
                if($item['2'] <=0){
                    continue;
                }
                $arr['user_id'] = $this->shop['id'];
                $arr['goods_id'] = $item[0];
                $arr['goods_attr_id'] = $item[1];
                $arr['goods_num'] = $item[2];
                $arr['is_del'] = 0;
                $arr['status'] = 0;
                $arr['type'] = 0;
                $arr['create_time'] = time();
                $arr['update_time'] = time();
                array_push($new_data,$arr);
            }

            //这个地方是批量添加
            $key = ['user_id','goods_id','goods_attr_id','goods_num','is_del','status','type','create_time','update_time'];
            $re = ShoppingCart::getDb()->createCommand()->batchInsert('shopping_cart',$key,$new_data)->execute();
            if($re){
                return $this->renderJson([],'加购物车成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'加购物车失败',ConstantService::$response_fail);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * 删除进货单数据
     */
    public function actionDelShoppingCart(){
        if($this->isPost()){
            $ids = $this->post('ids');
            $ids = explode(',',$ids);
            if(empty($ids) || !is_array($ids)){
                return $this->renderJson([],'参数错误',ConstantService::$response_fail);
            }
            $re = ShoppingCart::updateAll(['is_del'=>1],['id'=>$ids]);
            if($re){
                return $this->renderJson([],'删除成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'删除失败',ConstantService::$response_fail);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * 修改进货单
     */
    public function actionModifyShoppingCart(){
        if($this->isPost()){
            $id = $this->post('id');
            $goods_num = $this->post('goods_num');
            $cart = ShoppingCart::findOne(['id'=>$id]);
            $cart->goods_num = $goods_num;
            $re = $cart->save();
            if($re){
                return $this->renderJson([],'操作成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'操作失败',ConstantService::$response_fail);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * 获取进货单列表
     */
    public function actionGetLists(){
        if($this->isGet()){
            $query = ShoppingCart::find();
            $query->andWhere(['is_del' => 0]);
            $query->andWhere(['status' => 0]);
            $query->andWhere(['user_id' => $this->shop['id']]);
//            $p = intval($this->get('page', 1));
//            $p = ($p > 0) ? $p : 1;

//            $offset = ($p - 1) * $this->page_size;
            $lists = $query->orderBy(['update_time' => SORT_DESC])
//                ->offset($offset)
//                ->limit($this->page_size)
                ->asArray()
                ->all();
            //获取商品名称
            $goods = [];
            if(!empty($lists)){
                $goods_id = array_column($lists,'goods_id');
                $goods_attr_id = array_column($lists,'goods_attr_id');
                $goods = SupplierGoods::find()
                    ->select(['id','goods_name','status','goods_type','is_del','list_img'])
                    ->where(['id'=>$goods_id])
                    ->asArray()
                    ->all();
                unset($item);
                $goods_attrs = SupplierGoodsAttr::find()
                    ->where(['id'=>$goods_attr_id])
                    ->asArray()
                    ->all();
                $new_goods_attrs = SupplierGoodsService::getAttributeValue($goods_attrs);

                $new_list = [];
                foreach ($lists as $item){
                    $item['goods_attr'] = [];
                    if(isset($new_goods_attrs[$item['goods_attr_id']]) && !empty($new_goods_attrs[$item['goods_attr_id']])){
                        $new_goods_attrs[$item['goods_attr_id']]['image'] = GlobalUrlService::buildCdnUrl($new_goods_attrs[$item['goods_attr_id']]['image']);
                        $item['goods_attr'] = $new_goods_attrs[$item['goods_attr_id']];
                    }

                    $new_list[$item['goods_id']][] = $item;
                }
                unset($item);

                foreach ($goods as &$item){
                    $item['list_img'] = GlobalUrlService::buildCdnUrl($item['list_img']);
                    if(isset($new_list[$item['id']]) && !empty($new_list[$item['id']])){
                        $item['cart'] = $new_list[$item['id']];
                    }
                }
            }

            return $this->renderJson($goods, '', ConstantService::$response_success);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);

    }

    /**
     * 根据进货单中商品分类随机10个商品
     */
    public function actionRandGoods(){
        if($this->isGet()){
            $query = ShoppingCart::find();
            $query->andWhere(['is_del' => 0]);
            $query->andWhere(['status' => 0]);
            $query->andWhere(['type' => 0]);
            $lists = $query->orderBy(['update_time' => SORT_DESC])
                ->offset(0)
                ->limit(10)
                ->asArray()
                ->all();
            $goods_query = SupplierGoods::find();
            $goods_query->andWhere(['status'=>1]);
            $goods_query->andWhere(['is_del'=>0]);
            if(!empty($lists)){
                $goods_id = array_column($lists,'goods_id');
                $currents = $goods_query->where(['id'=>$goods_id])->asArray()->all();
                $tree_ids = array_column($currents,'tree_id');
                $goods_query->andWhere(['!=','id',$goods_id]);
                $goods_query->andWhere(['tree_id' => $tree_ids]);

            }
            $goods = $goods_query->offset(0)
                ->limit(10)
                ->orderBy(['update_time' => SORT_DESC])
                ->asArray()
                ->all();
            $goods = SupplierGoodsService::GetAttrListPrice($goods);

            return $this->renderJson($goods,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

}